package com.eqdd.yiqidian.ui.select;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.RecyclerViewCustom;
import com.eqdd.yiqidian.adapter.recycleadapter.ItemClickSupport;
import com.eqdd.yiqidian.adapter.slimadapter.SlimAdapterEx;
import com.eqdd.yiqidian.adapter.slimadapter.SlimInjector;
import com.eqdd.yiqidian.adapter.slimadapter.viewinjector.IViewInjector;
import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.rebuild.CareerListResultBean;
import com.eqdd.yiqidian.http.DialogCallBack;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Flowable;

public class SelectMultiPostActivity extends BaseActivity {
    private RecyclerViewCustom dataBinding;
    private SlimAdapterEx<CareerListResultBean> slimAdapterEx;

    private int departId;
    private int comId;

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_recyclerview);
        initTopTitleBar(View.VISIBLE, "职位选择");
        initTopRightText("完成", v -> {
            ArrayList<CareerListResultBean> resultList = new ArrayList<>();
            Flowable.fromIterable(slimAdapterEx.getData())
                    .filter(CareerListResultBean::isCheck)
                    .subscribe(resultList::add);
            if (!resultList.isEmpty()) {
                Intent intent = new Intent();
                intent.putParcelableArrayListExtra(Config.LIST, resultList);
                setResult(Config.SUCCESS, intent);
                finish();
            } else {
                ToastUtil.showShort("请至少选择一个!");
            }

        });
    }

    @Override
    public void initData() {
        comId = getIntent().getIntExtra(Config.COM_ID, 0);
        departId = getIntent().getIntExtra(Config.DEPARTMENT_ID, 0);
        dataBinding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        slimAdapterEx = SlimAdapterEx.create()
                .register(R.layout.list_item_25, new SlimInjector<CareerListResultBean>() {
                    @Override
                    public void onInject(CareerListResultBean data, IViewInjector injector) {
                        injector.text(R.id.tv_content, data.getName())
                                .checked(R.id.checkbox, data.isCheck())
                                .gone(R.id.tv_right);
                    }
                })
                .attachTo(dataBinding.recyclerView).updateData(new ArrayList());
        ItemClickSupport.addTo(dataBinding.recyclerView)
                .setOnItemClickListener((recyclerView, position, v) -> {
                    CareerListResultBean dataItem = slimAdapterEx.getDataItem(position);
                    dataItem.setCheck(!dataItem.isCheck());
                    slimAdapterEx.notifyItemChanged(position);
                });

    }


    @Override
    public void setView() {
        OkGo.<HttpResult<List<CareerListResultBean>>>post(HttpConfig.BASE_URL + HttpConfig.SELECT_CAREER)
                .params("departId", departId)
                .params("companyId", comId)
                .execute(new DialogCallBack<HttpResult<List<CareerListResultBean>>>(this) {
                    @Override
                    public void onSuccess(Response<HttpResult<List<CareerListResultBean>>> response) {
                        HttpResult<List<CareerListResultBean>> httpResult = response.body();
                        ToastUtil.showShort(httpResult.getMsg());
                        if (httpResult.getStatus() == 200) {
                            slimAdapterEx.updateData(httpResult.getItems());
                        }
                    }
                });
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {

        }
    }
}
